/*
 *  Player.cpp
 *  SquirrelAdven
 *
 *  Created by Wen Xu on 3/14/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "Player.h"
#include "OBJtoTriMesh.h"
#include <sstream>

Player::Player() {
	playerID = 0;
	playerMesh = NULL;
	direction = 0.0f;
//	inventory = NULL;
	
	numAcorns = 0;
	numSticks = 0;
	numRope = 0;
	numLogs = 0;
	numRocks = 0;
	
	numPoints = 0;
}

Player::~Player() {
}

void Player::setPlayerID(int id) {
	playerID = id;
}

void Player::setObjFilename(std::string objFilename) {
	this->objFilename = objFilename;
}

void Player::setWmtfFilename(std::string wmtfFilename) {
	this->wmtfFilename = wmtfFilename;
	OBJtoTriMesh objToTriMesh;
	playerMesh = objToTriMesh.CreateMesh("./", objFilename, wmtfFilename);
	computeBoundingBox();
}

int Player::getPlayerID() {
	return playerID;
}

void Player::setDirection(float d) {
	direction = d;
}

float Player::getDirection() {
	return direction;
}

TriMesh * Player::getPlayerMesh() {
	return playerMesh;
}

std::vector<PlayerAction> Player::getActionList() {
	return actionList;
}

void Player::addAction(PlayerAction action) {
	actionList.push_back(action);
}
void Player::clearActionList() {
	actionList.clear();
}

void Player::computeBoundingBox() {
	float min_x, max_x, min_y, max_y, min_z, max_z;
	int i;
	int numVertices = playerMesh->GetNumVertices();
	
	// get the first vertice
	min_x = max_x = playerMesh->GetPosition(0)[0];
	min_y = max_y = playerMesh->GetPosition(0)[1];
	min_z = max_z = playerMesh->GetPosition(0)[2];
	
	for (i = 1; i < numVertices; i++) {
		Float3 v = playerMesh->GetPosition(i);
		
		// min x
		if (v[0] < min_x) {
			min_x = v[0];
		}
		
		// max x
		if (v[0] > max_x) {
			max_x = v[0];
		}
		
		// min y
		if (v[1] < min_y) {
			min_y = v[1];
		}
		
		// max y
		if (v[1] > max_y) {
			max_y = v[1];
		}
		
		// min z
		if (v[2] < min_z) {
			min_z = v[2];
		}
		
		// max z
		if (v[2] > max_z) {
			max_z = v[2];
		}
	}
	
	// compute length
	len_x = max_x - min_x;
	len_y = max_y - min_y;
	len_z = max_z - min_z;
	
	// compute center point
	//centerPoint[0] = (min_x + max_x) / 2;
	//centerPoint[1] = (min_y + max_y) / 2;
	//centerPoint[2] = (min_z + max_z) / 2;
	
	centerPoint[0] = 0;
	centerPoint[1] = 0;
	centerPoint[2] = 0;
	
	
	float half_len_x = len_x / 2;
	float half_len_z = len_z / 2;
	radius = half_len_x > half_len_z ? half_len_x :half_len_z;
	
}

float Player::getOriginalLengthX() {
	return len_x;
}

float Player::getOriginalLengthY() {
	return len_y;
}

float Player::getOriginalLengthZ() {
	return len_z;
}


float Player::getRadius() {
	return radius;
}

Float3 Player::getCenterPoint() {
	return centerPoint;
}

void Player::modifyRadius(APoint scale_p) {
	float new_len_x = len_x * scale_p[0];
	float new_len_z = len_z * scale_p[2];
	
	float half_len_x = new_len_x / 2;
	float half_len_z = new_len_z / 2;
	radius = half_len_x > half_len_z ? half_len_x :half_len_z;
}

void Player::modifyCenterPoint(APoint translate_p) {
	centerPoint[0] += translate_p[0];
	centerPoint[1] += translate_p[1];
	centerPoint[2] += translate_p[2];
}

void Player::addToInventory(Items * item)
{
	int itemType = item->getItemType();
	std::cout<<"Got here. Itemtype: " << itemType << "\n";
	switch (itemType)
	{
		case 0:
			numSticks++;
			numPoints += 5;
			std::cout<<"Picked up a stick\n";
			break;
		case 1:
			numAcorns++;
			numPoints += 5;
			std::cout<<"Picked up an acorn\n";
			break;
		case 2:
			numRope++;
			numPoints += 5;
			std::cout<<"Picked up a rope\n";
			break;
		case 3:
			numLogs++;
			numPoints += 5;
			std::cout<<"Picked up a log\n";
			break;
		case 4:
			numRocks++;
			numPoints += 5;
			std::cout<<"Picked up a rock\n";
			break;
		default:
			break;
	}
	
	std::cout<< "Number of Sticks:\t" << numSticks << "\n";
	std::cout<< "Number of Acorns:\t" << numAcorns << "\n";
	std::cout<< "Number of Ropes:\t" << numRope << "\n";
	std::cout<< "Number of Logs:\t\t" << numLogs << "\n";
	std::cout<< "Number of Rocks:\t" << numRocks << "\n";
}

void Player::removeFromInventory(Items * item)
{
	int itemType = item->getItemType();
	
	switch (itemType)
	{
		case '0':
			if (numSticks > 0) {
				numSticks--;
			}
			break;
		case '1':
			if (numAcorns > 0) {
				numAcorns--;
			}
			break;
		case '2':
			if (numRope > 0) {
				numRope--;
			}
			break;
		case '3':
			if (numLogs > 0) {
				numLogs--;
			}
			break;
		case '4':
			if (numRocks > 0) {
				numRocks--;
			}
			break;
		default:
			break;
	}
}

std::string Player::printInventory()
{
	std::string mInventoryMessage;
	stringstream ss;
//	std::ostringstream oss;
//	sprintf(tempStr, "%d", numSticks);
//	oss << "Sticks\n" << numSticks << "\n";
//	mInventoryMessage.append(oss.str());
	ss << "|\tAcorns:\t" << numAcorns;
	mInventoryMessage.append(ss.str());
	ss.str("");
	ss << "\t|\tRope:\t" << numRope;
	mInventoryMessage.append(ss.str());
	ss.str("");
	ss << "\t|\tLogs:\t\t" << numLogs;
	mInventoryMessage.append(ss.str());
	ss.str("");
	ss << "\t|\tRocks:\t" << numRocks << "\t|";
	mInventoryMessage.append(ss.str());
	ss.str("");
	
	return mInventoryMessage;
}

int Player::totalPoints()
{
	return numPoints;
}

void Player::addPoints(int points) {
	numPoints += points;
}